package com.ficus.road.maintain.third.zh.cleaning;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.*;
import com.ficus.road.maintain.dao.MechanicalCleaningDao;
import com.ficus.road.maintain.dao.RoadInfoAdditionalDao;
import com.ficus.road.maintain.dao.RoadInfoDao;
import com.ficus.road.maintain.util.MyDateUtil;
import com.ficus.road.maintain.third.zh.BaseZH;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/4/15 下午5:01
 */
@Component
public class ZHMechanicalCleaningRoad extends BaseZH {

    @Autowired
    private RoadInfoDao roadInfoDao;
    @Autowired
    private RoadInfoAdditionalDao roadInfoAdditionalDao;
    @Autowired
    private MechanicalCleaningDao mechanicalCleaningDao;

    @PostConstruct
    void init() {
        arg0 = "ytkjCleanStasticsByRoad";
        errMsg = "调用中海接口获取机械保洁数据异常!";
        successMsg = "调用中海接口获取机械保洁数据成功!";
    }

    //    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "0 0 12,14,16 * * ?")
    public void refreshCleaningRoad() {
        String dateStr = MyDateUtil.dateFormatStr(new Date(), "yyyy-MM-dd");
        arg1 = "{\"datetime\":\"" + dateStr + "\"}";
        String cleaningRoadStr = getZHData();
        if (ObjectUtils.isEmpty(cleaningRoadStr)) {
            return;
        }
        List<MechanicalCleaningZH> mechanicalCleaningZHList = JSONArray.parseArray(cleaningRoadStr, MechanicalCleaningZH.class);
        List<MechanicalCleaning> mechanicalCleaningList = mechanicalCleaningZHList.parallelStream().map(item -> convert2MechanicalCleaning(item)).collect(Collectors.toList());
        mechanicalCleaningList.parallelStream().forEach(item -> {
            mechanicalCleaningDao.saveOrUpdate(item);
        });
    }

    private MechanicalCleaning convert2MechanicalCleaning(MechanicalCleaningZH zh) {
        Query query = new Query(Criteria.where("sec_num").is(zh.getRoadid()).and("unit_id").is(zh.getManagesectionid()));
        RoadInfo roadInfo = roadInfoDao.findOne(query);
        List<Coordinate> coordinates = null;
        if (ObjectUtils.isNotEmpty(roadInfo)) {
            coordinates = roadInfo.getCoordinates();
        } else {
            RoadInfoAdditional roadInfoAdditional = roadInfoAdditionalDao.findOne(query);
            if (ObjectUtils.isNotEmpty(roadInfoAdditional)) {
                coordinates = roadInfoAdditional.getCoordinateList();
            } else {
                System.out.println(zh.getRoadid() + " " + zh.getManagesectionid());
            }
        }
        MechanicalCleaning cleaning = MechanicalCleaning.builder()
                .id(zh.getDatatime() + "-" + zh.getRoadid() + "-" + zh.getManagesectionid() + "-" + zh.getStartlocationdesc() + "-" + zh.getEndlocationdesc())
                .date(zh.getDatatime())
                .startEnd(zh.getStartlocationdesc() + "-" + zh.getEndlocationdesc())
                .sectionName(zh.getManagesectionname())
                .unitCompany(zh.getUnitname())
                .workRate(String.format("%.4f", Double.parseDouble(zh.getWorkrate())))
                .isOk(zh.getIsworklengthok().equals("0") ? "不合格" : "合格")
                .roadName(zh.getRoadname())
                .modificationTime(MyDateUtil.dateFormatStr(new Date(), "yyyy-MM-dd HH:mm:ss"))
                .coordinates(coordinates)
                .build();
        return cleaning;
    }
}
